package do

import (
	"micro.tag.server/common"
	"micro.tag.server/database"
	"micro.tag.server/models"
)

const (
	HotelDBTable = "vs_h_tags"
)

func CreateHotelTag(tag *models.VsHTags) error {
	eg := database.GetDB()
	if eg == nil {
		return common.DBNil
	}

	afterID, err := eg.Table(HotelDBTable).Insert(tag)
	if err != nil {
		return err
	}
	if afterID < 1 {
		return common.DBInsertErr
	}

	return nil
}

func DeleteHotelTag(hotelID uint64, tagIDs []uint64) error {
	eg := database.GetDB()
	if eg == nil {
		return common.DBNil
	}

	tag := models.VsHTags{}
	afterID, err := eg.Table(HotelDBTable).Where("hotel_id = ?", hotelID).In("id", tagIDs).Delete(&tag)
	if err != nil {
		return err
	}
	if afterID < 1 {
		return common.DBDeleteErr
	}

	return nil
}

func HotelTags(hotelID uint64) ([]*models.VsHTags, error) {
	eg := database.GetDB()
	if eg == nil {
		return nil, common.DBNil
	}

	tags := make([]*models.VsHTags, 0)
	err := eg.Table(HotelDBTable).Where("hotel_id = ?", hotelID).Find(tags)
	if err != nil {
		return nil, err
	}

	return tags, nil
}
